package com.yaj.myfactorysupport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaj.myfactorysupport.entity.TmReason;
import com.yaj.myfactorysupport.mapper.TmReasonMapper;
import com.yaj.myfactorysupport.service.TmReasonService;
import com.yaj.myfactorysupport.util.ErrorCodeEnum;
import com.yaj.myfactorysupport.util.RespBean;
import com.yaj.myfactorysupport.vo.TmReasonVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wei
 * @since 2021-12-27
 */
@Service
public class TmReasonServiceImpl extends ServiceImpl<TmReasonMapper, TmReason> implements TmReasonService {
    @Resource
    private TmReasonMapper tmReasonMapper;

    @Override
    public RespBean getReasonList() {
        List<TmReason> tmReasons = tmReasonMapper.selectList(new QueryWrapper<TmReason>().lambda().eq(TmReason::getActiveFlag,1));
        List<TmReasonVo> tmReasonVos =  new ArrayList<TmReasonVo>();

        if(ObjectUtil.isNull(tmReasons)){
            return RespBean.error(ErrorCodeEnum.无法取得理由一览.getCode(),ErrorCodeEnum.无法取得理由一览.getMsg());
        }

        for(TmReason tmReason:tmReasons){
            TmReasonVo tmReasonVo = new TmReasonVo();
            BeanUtil.copyProperties(tmReason,tmReasonVo);
            tmReasonVos.add(tmReasonVo);
        }

        return RespBean.success(tmReasonVos);
    }
}
